* -------------------------------------- * 
* Table 2, Panel A: Hire Rate Comparison *
* -------------------------------------- * 

clear
use ${data}simulation_results.dta



* Collapse to one observation per teacher per bootstrap-outside option combination
collapse (max) hire_fo = fo_match  hire_da  =da_match, by(teacher_id oo bs) 

* Average hire rate in each bootstrap iteration (across the 10 outside option draws for schools)
collapse (mean) hire_fo  hire_da , by( bs )

gen effect = hire_da-hire_fo
matrix hires = J(3,4,.)
su hire_fo if bs==0
matrix hires[1,1] = r(mean)
su hire_da if bs==0
matrix hires[2,1] = r(mean)
su effect if bs==0
matrix hires[3,1] = r(mean)
su effect 
matrix hires[3,2] = r(sd)
sort effect
matrix hires[3,3] = .5*(effect[12]+effect[13])
matrix hires[3,4] = .5*(effect[488]+effect[489])


clear
svmat hires
outsheet using ${results}table2_panelA_hireRate.csv, comma names replace




* -------------------------------------------- *
* Table 2, Panel A: Distribution of Hire Rounds * 
* -------------------------------------------- *

clear
use ${data}simulation_results.dta

tab round_da, gen(r_da)
tab round_fo, gen(r_fo)



collapse (mean) r_da* r_fo*, by( bs) fast


matrix rounds = J(7,6,.)
forv r=1/7 {
	replace r_da`r' = 100*r_da`r'
	replace r_fo`r' = 100*r_fo`r'
	su r_da`r' if bs==0
	matrix rounds[`r',1] = r(mean)
	su r_fo`r' if bs==0
	matrix rounds[`r',2] = r(mean)
	gen e`r' = (r_da`r'-r_fo`r')
	su e`r' if bs==0
	matrix rounds[`r',3] = r(mean)
	su e`r'
	matrix rounds[`r',4] = r(sd)
	sort e`r'
	matrix rounds[`r',5] = .5*(e`r'[12]+e`r'[13])
	matrix rounds[`r',6] = .5*(e`r'[488]+e`r'[489])
	
}
clear
svmat rounds
outsheet using ${results}table2_panelA_hireRounds.csv, comma names replace




* ------------------------------------------------------------------ *
* Table 3, Panel A: Teachers who benefit or are hurt by DA           * 
* Figure 2, Panel A: Impact of DA on Teachers' Average Rank of Match *
* ------------------------------------------------------------------ *

clear
use ${data}simulation_results.dta

* Focus on actual sample for this exercise (still have 10 school outside options)
keep if bs==0 


collapse (mean) rank_cm_fo rank_cm_da  yearsSinceCollege female  black hispanic white otherRace gpa undergrad_satmt75   major_mathScience major_humanities  doubleMajor undergrad_sat_ms, by(teacher_id)

* Indicator for having an above median benefit
gen change_rank = rank_cm_fo-rank_cm_da
 replace change_rank = 0 if missing(rank_cm_fo) & missing(rank_cm_da)
 
 

* Generate Figure 2, Panel A
graph twoway (hist change_rank, fcolor(gs5) lcolor(gs5) frac ) , ///
	 graphregion(color(white)) ///
	xtitle("Change in Teacher's Rank of Match (FO-DA)") ytitle("Share of Teachers")  ylabel(,nogrid)
graph export ${results}figure2_panelA.png, replace
 
 
 
* Generate indicator for having an above median benefit 
qui su change_rank, d
local median = r(p50)
gen benefit = change_rank >`median'


* Regression estimates
reg benefit yearsSinceCollege female black hispanic  otherRace gpa undergrad_satmt75   major_mathScience major_humanities  doubleMajor undergrad_sat_ms , r 
eststo teacher_benefits
  
  estout teacher_benefits ///
	using ${results}table3_panelA_reg.txt, replace ///
	 cells(b(star fmt(2) nostar)  se(par fmt(3))) ///
	collabels(,none) stat(N)	
	
	
* Means
local i=0
matrix teacher_benefits = J(15,3,.)
 foreach var of varlist change_rank yearsSinceCollege female black hispanic white otherRace gpa undergrad_satmt75   major_mathScience major_humanities  doubleMajor undergrad_sat_ms {
 	local i=`i'+1
	su `var' if benefit==0
	matrix teacher_benefits[`i',1] = r(mean)
	su `var' if benefit==1
	matrix teacher_benefits[`i',2] = r(mean)	
 	reg `var' benefit, r
	matrix teacher_benefits[`i',3] = 2*ttail(e(df_r),abs(_b[benefit]/_se[benefit]))
 }
 local i=`i'+1
 count if benefit==0
 matrix teacher_benefits[`i',1] = r(N)
 count if benefit==1
 matrix teacher_benefits[`i',2] = r(N)
 

svmat teacher_benefits
local i=0
gen varname = ""
foreach var of varlist change_rank yearsSinceCollege female black hispanic white otherRace gpa undergrad_satmt75   major_mathScience major_humanities  doubleMajor undergrad_sat_ms  {
	local i=`i'+1
	replace varname = "`var'" in `i'
}
keep varname teacher_benefits*
ren teacher_benefits1 hurt
ren teacher_benefits2 benefit
ren teacher_benefits3 p
keep in 1/14
outsheet using ${results}table3_panelA_means.txt, comma names replace




* ----------------------------------------------------------- *
* Table 3, Panel B: Schools who benefit or are hurt by DA     *
* Figure 2, Panel B: Impact of DA on Number of Schools' Hires *
* ----------------------------------------------------------- *


clear
use ${data}simulation_results.dta

* Focus on actual sample for this exercise (still have 10 school outside options)
keep if bs==0


collapse  (mean) sch_n_rethires_da sch_n_rethires_fo sch_n_hires_fo sch_n_hires_da enroll_std Charter  Traditional Alternative     majority_black  majority_hispanic  no_majority latitude_std longitude_std latlon_std , by(school_id )


* Figure 2, Panel B
gen change_n_hires_noretention = sch_n_hires_da-sch_n_hires_fo

graph twoway (hist change_n_hires_noretention , fcolor(gs10) lcolor(gs10) frac )  , ///
	 graphregion(color(white)) ///
	xtitle("Change in  Number of Hires") ytitle("Share of Schools")  ylabel(,nogrid)
graph export ${results}figure2_panelB.png, replace


* Indicator for benefitting
gen change_n_hires = sch_n_rethires_da-sch_n_rethires_fo
gen benefit = change_n_hires>0



* Regression 
reg benefit enroll_std Charter   Alternative     majority_black  majority_hispanic   latitude_std longitude_std latlon_std , r 
eststo school_benefits
  
  estout school_benefits ///
	using ${results}table3_panelB_reg.txt, replace ///
	 cells(b(star fmt(3) nostar)  se(par fmt(3))) ///
	collabels(,none) stat(N )	

* Means
local i=0
matrix school_benefits = J(12,3,.)
 foreach var of varlist change_n_hires enroll_std Charter  Traditional Alternative     majority_black  majority_hispanic  no_majority latitude_std longitude_std latlon_std {
 	local i=`i'+1
	su `var' if benefit==0
	matrix school_benefits[`i',1] = r(mean)
	su `var' if benefit==1
	matrix school_benefits[`i',2] = r(mean)	
 	reg `var' benefit, r
	matrix school_benefits[`i',3] = 2*ttail(e(df_r),abs(_b[benefit]/_se[benefit]))
 }
 
 local i=`i'+1
 count if benefit==0
 matrix school_benefits[`i',1] = r(N)
 count if benefit==1
 matrix school_benefits[`i',2] = r(N)



svmat school_benefits
local i=0
gen varname = ""
foreach var of varlist change_n_hires enroll_std Charter  Traditional Alternative     majority_black  majority_hispanic  no_majority latitude_std longitude_std latlon_std  {
	local i=`i'+1
	replace varname = "`var'" in `i'
}
keep varname school_benefits*
ren school_benefits1 hurt
ren school_benefits2 benefit
ren school_benefits3 p
outsheet using ${results}table3_panelB_means.txt, comma names replace





* -------------------------------------------------------------------------- *
* Table 4, Panel A: Match Outcomes Under Alternative Mechanisms for Teachers *
* -------------------------------------------------------------------------- *

clear
use ${data}simulation_results.dta



collapse  teacher_hire_fo teacher_hire_da  teacher_hire_tp  teacher_hire_da_tier teacher_hire_rsd teacher_hire_draft rank_cm_fo rank_cm_da rank_cm_tp rank_cm_da_tier rank_cm_rsd rank_cm_draft retain_fo retain_da retain_tp retain_da_tier retain_rsd retain_draft, by(bs )



matrix cm_res = J(6,12,.)
local row=0
foreach var of varlist  teacher_hire_fo teacher_hire_da  teacher_hire_tp teacher_hire_da_tier teacher_hire_rsd teacher_hire_draft {
	local row=`row'+1
	su `var' if bs==0
	matrix cm_res[`row',1] = r(mean)
	su `var'  
	matrix cm_res[`row',2] = r(sd)
	sort `var'
	matrix cm_res[`row',3] = .5*(`var'[12]+`var'[13])
	matrix cm_res[`row',4] = .5*(`var'[488]+`var'[489])
	
	
}
local row=0
foreach var of varlist rank_cm_fo rank_cm_da rank_cm_tp rank_cm_da_tier rank_cm_rsd rank_cm_draft {
	local row=`row'+1
	su `var' if bs==0
	matrix cm_res[`row',5] = r(mean)
	su `var' 
	matrix cm_res[`row',6] = r(sd)
	sort `var'
	matrix cm_res[`row',7] = .5*(`var'[12]+`var'[13])
	matrix cm_res[`row',8] = .5*(`var'[488]+`var'[489])
}

local row=0
foreach var of varlist retain_fo retain_da retain_tp retain_da_tier retain_rsd retain_draft {
	local row=`row'+1
	su `var' if bs==0
	matrix cm_res[`row',9] = r(mean)
	su `var' 
	matrix cm_res[`row',10] = r(sd)
	sort `var'
	matrix cm_res[`row',11] = .5*(`var'[12]+`var'[13])
	matrix cm_res[`row',12] = .5*(`var'[488]+`var'[489])
}

clear
svmat cm_res
outsheet using ${results}table4_panelA.csv, comma names replace


* ------------------------------------------------------------------------- *
* Table 4, Panel B: Match Outcomes Under Alternative Mechanisms for Schools *
* ------------------------------------------------------------------------- *


clear
use ${data}simulation_results.dta

* Collapse from position level to school level
collapse (sum) fo_match da_match tp_match da_tier_match rsd_match draft_match retain_fo retain_da retain_tp  retain_da_tier retain_rsd retain_draft , by(bs intDay oo school_id)


foreach t in fo da tp da_tier rsd draft {
	gen any_`t' = `t'_match>0 & `t'_match<.
	gen any_keep_`t' = retain_`t'>0 & retain_`t'<.
}



collapse  any_fo any_da any_tp any_da_tier any_rsd any_draft  fo_match da_match tp_match da_tier_match rsd_match draft_match retain_fo retain_da retain_tp retain_da_tier retain_rsd retain_draft, by(bs )


matrix school_res = J(6,12,.)
local row=0
foreach var of varlist   any_fo any_da any_tp any_da_tier any_rsd any_draft {
	local row=`row'+1
	su `var' if bs==0
	matrix school_res[`row',1] = r(mean)
	su `var' 
	matrix school_res[`row',2] = r(sd)
	sort `var'
	matrix school_res[`row',3] = .5*(`var'[12]+`var'[13])
	matrix school_res[`row',4] = .5*(`var'[488]+`var'[489])
	
}
local row=0
foreach var of varlist fo_match da_match tp_match da_tier_match rsd_match draft_match   {
	local row=`row'+1
	su `var' if bs==0
	matrix school_res[`row',5] = r(mean)
	su `var'
	matrix school_res[`row',6] = r(sd)
	sort `var'
	matrix school_res[`row',7] = .5*(`var'[12]+`var'[13])
	matrix school_res[`row',8] = .5*(`var'[488]+`var'[489])
}
local row=0
foreach var of varlist   retain_fo retain_da retain_tp retain_da_tier retain_rsd retain_draft {
	local row=`row'+1
	su `var' if bs==0
	matrix school_res[`row',9] = r(mean)
	su `var' 
	matrix school_res[`row',10] = r(sd)
	sort `var'
	matrix school_res[`row',11] = .5*(`var'[12]+`var'[13])
	matrix school_res[`row',12] = .5*(`var'[488]+`var'[489])
}

clear
svmat school_res
outsheet using ${results}table4_panelB.csv, comma names replace

exit
